package org.spiderflow.aria.util;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.spiderflow.aria.m3u8.download.M3u8DownloadFactory;
import org.spiderflow.aria.m3u8.listener.DownloadListener;
import org.spiderflow.aria.m3u8.utils.Constant;

import java.io.IOException;

//开启setter方法的链式调用
@Accessors(chain = true)
@Data
public class M3u8Downloader {

    private String url;
    private String dir;
    private String fileName;


    public void send() {

        M3u8DownloadFactory.M3u8Download m3u8Download = M3u8DownloadFactory.getInstance(url);
        if (StringUtils.isBlank(dir)) {
            dir = "/downloads";
        } else {
            dir = "/downloads/" + dir;
        }
        //设置生成目录
        m3u8Download.setDir(dir);
        //设置视频名称
        if (StringUtils.isBlank(fileName)) {
            Integer start = url.lastIndexOf("/") + 1;
            Integer end = url.lastIndexOf(".");
            fileName = url.substring(start, end);
        }
        m3u8Download.setFileName(fileName);
        //设置线程数
        m3u8Download.setThreadCount(10);
        //设置重试次数
        m3u8Download.setRetryCount(10);
        //设置连接超时时间（单位：毫秒）
        m3u8Download.setTimeoutMillisecond(10000L);
        /*
        设置日志级别
        可选值：NONE INFO DEBUG ERROR
        */
        m3u8Download.setLogLevel(Constant.INFO);
        //设置监听器间隔（单位：毫秒）
        m3u8Download.setInterval(500L);
        //添加额外请求头
      /*  Map<String, Object> headersMap = new HashMap<>();
        headersMap.put("Content-Type", "text/html;charset=utf-8");
        m3u8Download.addRequestHeaderMap(headersMap);*/
        //如果需要的话设置http代理
        //m3u8Download.setProxy("172.50.60.3",8090);
        //添加监听器
        m3u8Download.addListener(new DownloadListener() {
            @Override
            public void start() {
                System.out.println("开始下载！");
            }

            @Override
            public void process(String downloadUrl, int finished, int sum, float percent) {
                System.out.println("下载网址：" + downloadUrl + "\t已下载" + finished + "个\t一共" + sum + "个\t已完成" + percent + "%");
            }

            @Override
            public void speed(String speedPerSecond) {
                System.out.println("下载速度：" + speedPerSecond);
            }

            @Override
            public void end() {
                System.out.println("下载完毕");
            }
        });
        //开始下载
        m3u8Download.start();
    }


    public static void main(String[] args) throws IOException {
        String url = "https://a.kaicheba.cc/erkong/ng/凡人修仙传07.m3u8";
        url = "https://php.playerla.com/erkong/feiyun/dpm3u82.php?v=https://a.kaicheba.cc/erkong/ng/凡人修仙传07.m3u8&t=1646897259&ek=tk&k=d2fc02595371ad5786c5ac1312eafaef&vkey=B5E8287B1941FA8B665C12838E483B00250D4C52F029C2C2E6D25E41DD29F3D6C5";
        M3u8Downloader m3u8Downloader = new M3u8Downloader();
        m3u8Downloader.setUrl(url);
        m3u8Downloader.setDir("m3u8JavaTest");
        //m3u8Downloader.setFileName("test");
        m3u8Downloader.send();


    }

}
